package cn.com.sparknet.jsindustrymanager.provider;

import java.io.File;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public final class DBManager
{

    private DBManager()
    {
    }

    private static SQLiteDatabase openInputDatabase(Context context) throws InputDatabaseInvalidException
    {
        File inputFile = context.getFileStreamPath("JSIndustryManagerInput.db");
        try
        {
            return SQLiteDatabase.openDatabase(inputFile.getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        } catch (SQLiteException e)
        {
            throw new InputDatabaseInvalidException(e);
        }
    }

    public static InputNode queryFLFGInputNode(Context context, String id) throws InputDatabaseInvalidException
    {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try
        {
            db = openInputDatabase(context);
            String sql = null;
            if (id.equals("-1"))
            {
                sql = "select count(*) counts from t_flfg where parent_id is null";
                cursor = db.rawQuery(sql, null);
                cursor.moveToNext();
                long count = cursor.getLong(cursor.getColumnIndex("counts"));
                if (count == 0)
                    return null;
                InputNode node = new InputNode();
                if (count == 1)
                {
                    sql = "select id,title,content from t_flfg where parent_id is null";
                    cursor.close();
                    cursor = db.rawQuery(sql, null);
                    cursor.moveToNext();
                    node.title = cursor.getString(cursor.getColumnIndex("title"));
                    node.content = cursor.getString(cursor.getColumnIndex("content"));
                    String curId = cursor.getString(cursor.getColumnIndex("id"));
                    sql = "select id,title from t_flfg where parent_id = ? order by order_id,create_time";
                    cursor.close();
                    cursor = db.rawQuery(sql, new String[] { curId });
                    while (cursor.moveToNext())
                    {
                        node.children.add(new String[] { cursor.getString(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("title")) });
                    }
                    return node;
                } else
                {
                    node.title = "";
                    node.content = "";
                    sql = "select id,title from t_flfg where parent_id is null order by order_id,create_time";
                    cursor.close();
                    cursor = db.rawQuery(sql, null);
                    while (cursor.moveToNext())
                    {
                        node.children.add(new String[] { cursor.getString(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("title")) });
                    }
                    return node;
                }
            } else
            {
                sql = "select title,content from t_flfg where id = ?";
                cursor = db.rawQuery(sql, new String[] { id });
                if (!cursor.moveToNext())
                    return null;
                InputNode node = new InputNode();
                node.title = cursor.getString(cursor.getColumnIndex("title"));
                node.content = cursor.getString(cursor.getColumnIndex("content"));
                sql = "select id,title from t_flfg where parent_id = ? order by order_id,create_time";
                cursor.close();
                cursor = db.rawQuery(sql, new String[] { id });
                while (cursor.moveToNext())
                {
                    node.children.add(new String[] { cursor.getString(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("title")) });
                }
                return node;
            }
        } finally
        {
            try
            {
                if (cursor != null)
                    cursor.close();
            } finally
            {
                if (db != null)
                    db.close();
            }
        }
    }

    public static InputNode queryXCSCInputNode(Context context, String id) throws InputDatabaseInvalidException
    {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try
        {
            db = openInputDatabase(context);
            String sql = null;
            if (id.equals("-1"))
            {
                sql = "select count(*) counts from t_xcsc where parent_id is null";
                cursor = db.rawQuery(sql, null);
                cursor.moveToNext();
                long count = cursor.getLong(cursor.getColumnIndex("counts"));
                if (count == 0)
                    return null;
                InputNode node = new InputNode();
                if (count == 1)
                {
                    sql = "select id,title,content from t_xcsc where parent_id is null";
                    cursor.close();
                    cursor = db.rawQuery(sql, null);
                    cursor.moveToNext();
                    node.title = cursor.getString(cursor.getColumnIndex("title"));
                    node.content = cursor.getString(cursor.getColumnIndex("content"));
                    String curId = cursor.getString(cursor.getColumnIndex("id"));
                    sql = "select id,title from t_xcsc where parent_id = ? order by order_id,create_time";
                    cursor.close();
                    cursor = db.rawQuery(sql, new String[] { curId });
                    while (cursor.moveToNext())
                    {
                        node.children.add(new String[] { cursor.getString(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("title")) });
                    }
                    return node;
                } else
                {
                    node.title = "";
                    node.content = "";
                    sql = "select id,title from t_xcsc where parent_id is null order by order_id,create_time";
                    cursor.close();
                    cursor = db.rawQuery(sql, null);
                    while (cursor.moveToNext())
                    {
                        node.children.add(new String[] { cursor.getString(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("title")) });
                    }
                    return node;
                }
            } else
            {
                sql = "select title,content from t_xcsc where id = ?";
                cursor = db.rawQuery(sql, new String[] { id });
                if (!cursor.moveToNext())
                    return null;
                InputNode node = new InputNode();
                node.title = cursor.getString(cursor.getColumnIndex("title"));
                node.content = cursor.getString(cursor.getColumnIndex("content"));
                sql = "select id,title from t_xcsc where parent_id = ? order by order_id,create_time";
                cursor.close();
                cursor = db.rawQuery(sql, new String[] { id });
                while (cursor.moveToNext())
                {
                    node.children.add(new String[] { cursor.getString(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("title")) });
                }
                return node;
            }
        } finally
        {
            try
            {
                if (cursor != null)
                    cursor.close();
            } finally
            {
                if (db != null)
                    db.close();
            }
        }
    }

}
